﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using AutoMapper.QueryableExtensions;
using FwProject.AppManager;
using FwProject.EventAdvertising.Dto;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.EventAdvertising;
using Microsoft.EntityFrameworkCore;
using X.PagedList;

namespace FwProject.EventAdvertising
{
    //[AbpAuthorize]
    public class NoticeAppService: FwProjectAppServiceBase, INoticeAppService
    {
        private readonly IRepository<Notice, Guid> _repositoryNotice;
        public readonly NoticeManager _noticeManager;
        public IGuidGenerator _guid { get; set; }
        public IObjectMapper _map { get; set; }

        public NoticeAppService(IRepository<Notice, Guid> repositoryNotice, NoticeManager noticeManager)
        {
            _repositoryNotice = repositoryNotice;
            _guid = SequentialGuidGenerator.Instance;
            _map = NullObjectMapper.Instance;
            _noticeManager = noticeManager;
        }
        /// <summary>
        /// 获取公告消息
        /// </summary>
        /// <returns></returns>
        public async Task<List<NoticeDto>> GetNoticeListAsync()
        {
            var model = _repositoryNotice.GetAll().Where(o=>o.IsActive==true);
            return await model.OrderByDescending(o=>o.CreationTime).ProjectTo<NoticeDto>().ToListAsync();
        }

        #region 后台管理
        /// <summary>
        /// 添加一个公告
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Guid> Create(NoticeInput input)
        {
            var model = _map.Map<Notice>(input);
            model.Id = _guid.Create();
            return await _noticeManager.CreateNotice(model);
        }
        /// <summary>
        /// 编辑一个公告
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task Update(Guid id, NoticeInput input)
        {
            var model = await _repositoryNotice.GetAsync(id);
            model = _map.Map(input,model);
            await _noticeManager.UpdateNotice(model);
        }
        /// <summary>
        /// 修改启用状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isActive"></param>
        /// <returns></returns>
        public async Task UpdateActive(Guid id, bool isActive)
        {
            var model = await _repositoryNotice.GetAsync(id);
            model.IsActive = isActive;
            await _noticeManager.UpdateNotice(model);
        }
        /// <summary>
        /// 删除一个公告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task Delete(Guid id)
        {
            var model = await _repositoryNotice.GetAsync(id);
            await _noticeManager.DeleteNotice(model);
        }
        /// <summary>
        /// 获取一条公告
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<NoticeDto> GetByIdAsync(Guid id)
        {
            var model = await _repositoryNotice.GetAsync(id); ;
            return _map.Map<NoticeDto>(model);
        }

        /// <summary>
        /// 获取公告列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<LayerDataBaseDto> GetNoticeDataAsync(int page,int limit)
        {
            var model = _repositoryNotice.GetAll();

            var data = await model.OrderByDescending(o => o.CreationTime).ProjectTo<NoticeDto>().ToPagedListAsync(page,limit);
            LayerDataBaseDto dto = new LayerDataBaseDto();
            dto.Data = data;
            dto.Count = data.TotalItemCount;
            return dto;
        }
        #endregion
    }
}
